
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Controller," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>控制器（controller） [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        控制器（controller）
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Controller/" rel="tag">Ember-Controller</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 3 分钟</span>
    </header>

    <div class="post-content">
      <p>从本篇开始进入第五章控制器，<code>controller</code>在E<code>mber2.0</code>开始越来越精简了，职责也更加单一——处理逻辑。</p>
<p>下面是准备工作。<br>重新创建一个<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>项目，仍旧使用的是<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide">Ember CLI</a>命令创建。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ember new chapter5_controllers</span><br><span class="line">cd chapter5_controllers</span><br><span class="line">ember server</span><br></pre></td></tr></table></figure>
<p>在浏览器执行项目，看到如下信息说明项目搭建成功。<br><strong>Welcome to Ember</strong>。</p>
<h2 id="1，控制器简介"><a href="#1，控制器简介" class="headerlink" title="1，控制器简介"></a>1，控制器简介</h2><p>控制器与组件非常相似，由此，在未来的新版本中很有可能组件将会完全取代控制器，很可能随着Ember版本的更新控制器将退出<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>。目前的版本中组件还不能直接通过路由访问，需要通过模板调用才能使用组件，但是未来的版本会解决这个问题，到时候<code>controller</code>可能就真的从<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>退出了！</p>
<p>正因如此，模块化的Ember应用很少用到<code>controller</code>。即便是使用了<code>controller</code>也是为了处理下面的两件事情：</p>
<ol>
<li><code>controller</code>主要是为了维持当前路由状态。一般来说，model的属性会保存到服务器，但是<code>controller</code>的属性却不会保存到服务器。</li>
<li>组件上的动作需要通过<code>controller</code>层转到<code>route</code>层。</li>
</ol>
<p>模板上下文的渲染是通过当前<code>controller</code>的路由处理的。<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>所追随的理念是“约定优于配置”，这也就意味着如果你只需要一个<code>controller</code> 你就创建一个，而不是一切为了“便于工作”。</p>
<p>下面的例子是演示路由显示<code>blog post</code>。假设模板<code>blog-post</code>用于展示模型<code>blog-post</code>的数据，并在这个模型包含如下属性（隐含属性<code>id</code>，因为在<code>model</code>中不需要手动指定<code>id</code>属性）：</p>
<ul>
<li>title </li>
<li>intro</li>
<li>body</li>
<li>author</li>
</ul>
<p><code>model</code>定义如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/models/blog-post.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> DS <span class="keyword">from</span> <span class="string">&#x27;ember-data&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> DS.Model.extend(&#123;</span><br><span class="line">  title: DS.attr(<span class="string">&#x27;string&#x27;</span>),  <span class="comment">//  属性默认为string类型，可以不指定</span></span><br><span class="line">  intro: DS.attr(<span class="string">&#x27;string&#x27;</span>),</span><br><span class="line">  body: DS.attr(<span class="string">&#x27;string&#x27;</span>),</span><br><span class="line">  author: DS.attr(<span class="string">&#x27;string&#x27;</span>)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>在<code>route</code>层增加测试数据，直接返回一个<code>model</code>对象。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/blog-post.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> blogPost = <span class="built_in">this</span>.store.createRecord(<span class="string">&#x27;blog-post&#x27;</span>, &#123;</span><br><span class="line">            title: <span class="string">&#x27;DEFINING A COMPONENT&#x27;</span>,  <span class="comment">//  属性默认为string类型，可以不指定</span></span><br><span class="line">            intro: <span class="string">&quot;Components must have at least one dash in their name. &quot;</span>,</span><br><span class="line">            body: <span class="string">&quot;Components must have at least one dash in their name. So blog-post is an acceptable name, and so is audio-player-controls, but post is not. This prevents clashes with current or future HTML element names, aligns Ember components with the W3C Custom Elements spec, and ensures Ember detects the components automatically.&quot;</span>,</span><br><span class="line">            author: <span class="string">&#x27;ubuntuvim&#x27;</span></span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 直接返回一个model，或者你可以返回promises，</span></span><br><span class="line">        <span class="keyword">return</span> blogPost;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>显示信息的模板如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/blog-post.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;model.title&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;model.author&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;intro&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;model.intro&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">hr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;body&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;model.body&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果你的代码没有编写错误那么也会得到如下结果：</p>
<p><img src="http://static.oschina.net/uploads/img/201510/15003412_jEnj.png" alt="结果"></p>
<p><strong>Welcome to Ember</strong>是主模板的信息，你可以在<code>application.hbs</code>中删除，但是记得不要删除<code>&#123;&#123;outlet&#125;&#125;</code>，否则什么信息也不显示。</p>
<p>这个例子中没有显示任何特定的属性或者指定的动作（<code>action</code>）。此时，控制器的model属性所扮演的角色仅仅是模型属性的<code>pass-through</code>（或代理）。<br>注意：控制器获取的<code>model</code>是从<code>route</code>得到的。</p>
<p>下面为这个例子增加一个功能：用户可以点击标题触发显示或者隐藏<code>post</code>的内容。通过一个属性<code>isExpanded</code>控制，下面分别修改模板和控制器的代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/controllers/blog-post.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    isExpanded: <span class="literal">false</span>,  <span class="comment">//默认不显示body</span></span><br><span class="line">    actions: &#123;</span><br><span class="line">        toggleBody: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">this</span>.toggleProperty(<span class="string">&#x27;isExpanded&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>在<code>controller</code>中增加一个属性<code>isExpanded</code>，如果你不在<code>controller</code>中定义这个属性也是可以的。对于这个<code>controller</code>代码的解释请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/22/ember-js-ru-men-zhi-nan-zhi-shi-wu-action/">Ember.js 入门指南之十五 助手</a>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/blog-post.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;model.title&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;model.author&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;intro&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;model.intro&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">hr</span>&gt;</span></span><br><span class="line">&#123;&#123;#if isExpanded&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">toggleBody</span>&#x27;&#125;&#125;&gt;</span>hide body<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;body&quot;</span>&gt;</span></span><br><span class="line">        &#123;&#123;model.body&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">&#123;&#123;else&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">toggleBody</span>&#x27;&#125;&#125;&gt;</span>Show body<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">&#123;&#123;/if&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>在模板中使用<code>if</code>助手判断<code>isExpanded</code>的值，如果为<code>true</code>则显示<code>body</code>，否则不显示。</p>
<p>页面加载之后结果如下，首先是不显示<code>body</code>内容，点击按钮“Show body”则显示内容，并且按钮变为“hide body”。然后在点击这个按钮则不显示<code>body</code>内容。</p>
<p><img src="http://static.oschina.net/uploads/img/201510/15003412_4mzn.png" alt="隐藏"></p>
<p><img src="http://static.oschina.net/uploads/img/201510/15003412_atzS.png" alt="展开"></p>
<p>到此<code>controller</code>的职责你应该大致了解了，其主要的作用是逻辑的判断、处理，比如这里例子中判断<code>body</code>内容的显示与否，其实你也可以把<code>controller</code>类中的处理代码放在<code>route</code>类中也可以实现这个效果，但是要作为<code>model</code>的属性返回（把<code>isExpanded</code>当做<code>model</code>的属性处理），请读者自己动手试试，但是把逻辑放到<code>route</code>又会使得<code>route</code>变得“不专一”了，<code>route</code>的主要职责是初始化数据的。我想这也是Ember还留着<code>controller</code>的原因之一吧！！</p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！



    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/controllers/%E7%AE%A1%E7%90%86controller%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB/" rel="next" title="管理controller之间的依赖关系">
          管理controller之间的依赖关系
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/components/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%85%E8%A3%B9%E7%BB%84%E4%BB%B6%E7%9A%84HTML%E6%A0%87%E7%AD%BE/" rel="prev" title="自定义包裹组件的HTML标签">
            自定义包裹组件的HTML标签
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%EF%BC%8C%E6%8E%A7%E5%88%B6%E5%99%A8%E7%AE%80%E4%BB%8B"><span class="toc-text">1，控制器简介</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '控制器（controller）', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
